import json
from dataclasses import asdict

import requests
from config import HOST
from config import global_config
from core.task import Task


#HOST = 'http://127.0.0.1:5000'

def fetch_my_strategies():
    url = f"{HOST}/api/v1/strategies/my-strategies"
    user_token = global_config.user_token
    if not user_token:
        print('user_token不存在，请先在“配置中心”登录',)
        return
    headers = {
        "Authorization": f"{user_token}",  # 通常用 Bearer Token
        "Content-Type": "application/json",  # 可选，根据 API 要求
    }

    print(user_token, url)

    response = requests.get(url, headers=headers)
    print(response)
    try:
        data = response.json()
        print(data)
        return data
    except:
        print('获取自己的策略列表失败')
        import traceback
        traceback.print_exc()
        return None

def fetch_strategies():
    url = f"{HOST}/api/v1/strategies/?fields=name,author,is_official,points,access_type,is_vip_only"
    # user_token = global_config.user_token
    # if not user_token:
    #     print('user_token不存在，请先在“配置中心”登录',)
    #     return
    # headers = {
    #     "Authorization": f"{user_token}",  # 通常用 Bearer Token
    #     "Content-Type": "application/json",  # 可选，根据 API 要求
    # }
    #
    # print(user_token, url)

    response = requests.get(url)
    print(response)
    try:
        data = response.json()
        print(data)
        return data
    except:
        print('获取自己的策略列表失败')
        import traceback
        traceback.print_exc()
        return None

def fetch_list(data_type):
    if data_type == 'etf':
        url = f"{HOST}/api/v1/data/etf_list"
    if data_type == 'bond':
        url = f"{HOST}/api/v1/data/bond_list"

    #url = 'http://www.ailabx.com/api/v1/users/me'
    # url_data = 'http://127.0.0.1:5000/api/v1/data/etf_quotes/510300.SH?start_date=20250301&end_date=20250307'


    response = requests.get(url)

    status_code = response.status_code
    try:
        data = response.json()
    except:
        return None

    return data

def fetch_strategy(stragegy_id):
    url = f"{HOST}/api/v1/strategies/config/{stragegy_id}"
    user_token = global_config.user_token
    if not user_token:
        print('user_token不存在，请先在“配置中心”登录', )
        return
    headers = {
        "Authorization": f"{user_token}",  # 通常用 Bearer Token
        "Content-Type": "application/json",  # 可选，根据 API 要求
    }

    print(user_token, url)

    response = requests.get(url, headers=headers)
    #print(response)
    try:
        data = response.json()
        #print(data)
        return data,response.status_code
    except:
        print('获取自己的策略列表失败')
        import traceback
        traceback.print_exc()
        return response.text, response.status_code


def login(user_token):
    url = f'{HOST}/api/v1/users/me'
    #url = 'http://www.ailabx.com/api/v1/users/me'
    #url_data = 'http://127.0.0.1:5000/api/v1/data/etf_quotes/510300.SH?start_date=20250301&end_date=20250307'

    headers = {
        "Authorization": f"{user_token}",  # 通常用 Bearer Token
        "Content-Type": "application/json",  # 可选，根据 API 要求
    }

    print(user_token,url)

    response = requests.get(url, headers=headers)
    print(response)


    status_code = response.status_code
    try:
        data = response.json()
        print(data)
    except:
        return None

    return data,status_code


def backtest(task: Task):
    url = f"{HOST}/quant/run_backtest"

    response = requests.post(url,json=asdict(task))
    print(response)
    try:
        data = response.json()
        print(type(data),data)
        return data
    except:
        print('回测失败')
        import traceback
        traceback.print_exc()
        return None

if __name__ == '__main__':
    from gui.gui_logic import load_tasks
    from config import DATA_TASKS
    tasks = load_tasks(DATA_TASKS.resolve())
    backtest(task=tasks.get(list(tasks.keys())[0]))